package org.example.taskmaster.adapters;

import java.util.Calendar;

import org.example.taskmaster.R;
import org.example.taskmaster.R.color;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TaskListAdapter extends ArrayAdapter<String> {
	private final Context context;
	private final String[] names;
	//private final String[] dates;
	private final Integer[] complete;
	private final Integer[] days;
	private final Integer[] months;
	private final Integer[] years;

	public TaskListAdapter(Context context, String[] names, Integer[] days, Integer[] months, Integer[] years, Integer[] complete) {
		super(context, R.layout.listitem_layout, names);
		this.context = context;
		this.names = names;
		//this.dates = dates;
		this.complete = complete;
		this.days = days;
		this.months = months;
		this.years = years;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// create layout from XML
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.listitem_layout, parent, false);
		
		// get references to Views
		TextView nameView = (TextView) rowView.findViewById(R.id.listitem_label);
		TextView dateView = (TextView) rowView.findViewById(R.id.listitem_due);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.listitem_check);
		
		// set text
		//String content = names[position] + ": " + dates[position];
		nameView.setText(names[position]);
		String dateString = months[position].toString() + "/" + days[position].toString() + "/" + years[position].toString();
		dateView.setText(dateString);
		//dateView.setText(": " + dates[position]);

		// set colors according to completeness and whether due date is passed
		if (complete[position] == 1) {
			// task is complete
			nameView.setTextColor(Color.DKGRAY);
			dateView.setTextColor(Color.DKGRAY);
		} else {
			if (this.isPast(days[position],months[position],years[position])) {
				// incomplete and in the past
				nameView.setTextColor(Color.RED);
			} else {
				// incomplete and in the future
				nameView.setTextColor(Color.WHITE);
			}
		}
		
		// set check image
		Integer i = complete[position];
		if (i.equals(0)) { 
			// task is not complete
			imageView.setImageResource(R.drawable.greycheck);
		}
		else {
			imageView.setImageResource(R.drawable.greencheck);
		}

		return rowView;
	}
	private boolean isPast(int day, int month, int year) {
		// handle time interactions
		Calendar RightNow = Calendar.getInstance();
		int CurrentYear = RightNow.get(Calendar.YEAR);
		int CurrentMonth = RightNow.get(Calendar.MONTH) + 1;
		int CurrentDay = RightNow.get(Calendar.DAY_OF_MONTH);

		if (CurrentYear > year) {return true;}
		else if (CurrentMonth > month) {return true;}
		else if (CurrentDay > day) {return true;}
		else {return false;}
	}
	
}
